package com.bestcem.xm.qdes.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.qdes.controller.param.ExportQuotaParam;
import com.bestcem.xm.qdes.controller.param.QuotaCreateParam;
import com.bestcem.xm.qdes.controller.param.QuotaDeleteParam;
import com.bestcem.xm.qdes.controller.param.QuotaNameModifyParam;
import com.bestcem.xm.qdes.controller.vo.DeletedCountVo;
import com.bestcem.xm.qdes.controller.vo.QuotaIdVo;
import com.bestcem.xm.qdes.controller.vo.QuotaNameVo;
import com.bestcem.xm.qdes.entity.QdesQuota;
import com.bestcem.xm.qdes.service.QdesQuotaService;
import io.swagger.annotations.Api;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;

@Slf4j
@Api(tags = "问卷编辑-配额")
@RestController
@RequiredArgsConstructor
@RequestMapping("/qdes/projects")
public class ProjectQuotaController {
    private @NonNull QdesQuotaService qdesQuotaService;

    /**
     * 查询配额列表/单个配额
     *
     * @param projectId
     */
    @GetMapping(value = {"/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}", "/{projectId:[a-fA-F0-9]{24}}/quotas/"})
    public XmResultJson<Object> quotaGet(@ApiIgnore Page<QdesQuota> page, @PathVariable String projectId, @PathVariable(required = false) String quotaId) {
        //quataId不为空查单个详情
        if (StrUtil.isNotEmpty(quotaId)) {
            return XmResultJson.success(qdesQuotaService.getQuotaDetail(quotaId));
        }
        //查列表
        return XmResultJson.success(qdesQuotaService.getQuotaList(page, projectId));
    }

    /**
     * 修改单个配额，仅修改名称
     *
     * @param projectId
     */
    @PutMapping("/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}")
    public XmResultJson<QuotaIdVo> quotaModify(@PathVariable String projectId, @PathVariable String quotaId, @RequestBody QuotaNameModifyParam param) {
        return XmResultJson.success(qdesQuotaService.setQuotaName(projectId, quotaId, param));
    }

    /**
     * 创建配额
     *
     * @param projectId
     */
    @PostMapping("/{projectId:[a-fA-F0-9]{24}}/quotas")
    public XmResultJson<QuotaIdVo> quotaCreate(@PathVariable String projectId, @RequestBody QuotaCreateParam quotaCreateParam) {
        return XmResultJson.success(qdesQuotaService.createQuota(projectId, quotaCreateParam));
    }

    /**
     * 删除单个配额
     *
     * @param projectId
     */
    @DeleteMapping(value = {"/{projectId:[a-fA-F0-9]{24}}/quotas", "/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}"})
    public XmResultJson<DeletedCountVo> quotaDelete(@PathVariable String projectId,
                                                    @PathVariable(required = false) String quotaId, @RequestBody(required = false) QuotaDeleteParam quotaDeleteParam) {
        if (StringUtils.isNotEmpty(quotaId)) {
            QuotaDeleteParam deleteParam = new QuotaDeleteParam();
            deleteParam.setIdList(Collections.singletonList(quotaId));
            return XmResultJson.success(qdesQuotaService.deleteQuotas(projectId, deleteParam));
        }
        return XmResultJson.success(qdesQuotaService.deleteQuotas(projectId, quotaDeleteParam));
    }

    /**
     * 查询配额名字
     *
     * @param projectId
     * @param quotaId
     * @return
     */
    @GetMapping("/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/name")
    public XmResultJson<QuotaNameVo> quotaNameModify(@PathVariable String projectId, @PathVariable String quotaId) {
        return XmResultJson.success(qdesQuotaService.getQuotaName(projectId, quotaId));
    }

    /**
     * 导入配额
     *
     * @param projectId 项目id
     * @param files     files
     */
    @PutMapping("/{projectId:[a-fA-F0-9]{24}}/quotas/template")
    public XmResultJson<Object> importQuota(@PathVariable String projectId,
                                            @RequestParam(value = "quotafile") MultipartFile files) {
        return qdesQuotaService.importQuota(projectId, files);
    }

    /**
     * 导出配额
     *
     * @param projectId        项目id
     * @param exportQuotaParam param
     */
    @PostMapping("/{projectId:[a-fA-F0-9]{24}}/quotas/template")
    public void exportQuota(@PathVariable String projectId, @RequestBody ExportQuotaParam exportQuotaParam, HttpServletResponse response) {
        qdesQuotaService.exportQuota(projectId, exportQuotaParam, response);
    }
}
